import re

old_file = '/Users/timbloodzero/Java/dynamic_user.sql'
# f=open('路径/文件名', '读写格式', '编码方式', '错误处理方式')
# ‘r’ ：   Open a file for reading. (default默认)
# ‘w’：Open a file for writing. Creates a new file if it does not exist or truncates the file if it exists.
# ‘x’ ：Open a file for exclusive creation. If the file already exists, the operation fails.
# ‘a’ ：   Open for appending at the end of the file without truncating it. Creates a new file if it does not exist.
# ‘t’   ： Open in text mode. (default默认)
# ‘b’ ：   Open in binary mode.
# ‘+’ ：   Open a file for updating (reading and writing)

f = open(old_file, 'r', encoding='UTF-8', errors='ignore')
# 使用with语句，Python可以自动调用close()方法
# with open('/Users/timbloodzero/Java/dynamic_user.sql','r+') as f2:
#     print(f2.read())
#     f2.write('写入的内容')
text = f.read()
print(text)

# 保留原有文件带有hello的行并将原文件的hello替换成world
w_str = ""
print(f.__sizeof__())
for line in f:
    if re.search('CREATE', line):
        line = re.sub('CREATE', 'PRIMARY1`1', line)
        w_str += line
    else:
        w_str += line
print(w_str)
wopen = open(old_file, 'w')
wopen.write(w_str)
f.close()
